10. Exercise: Set up Paint for Drawing

22 09 AAK Paint Intro SC V2

Android Developer Documentation

Exercise

In this exercise you are going to set up the Paint for drawing.

  1. In MyCanvasView.kt, at the top file level, define a constant for the stroke width.
private const val STROKE_WIDTH = 12f // has to be float
  1. At the class level of MyCanvasView, define a variable drawColor for holding the color to draw with and initialize it with the colorPaint resource you defined earlier.
private val drawColor = ResourcesCompat.getColor(resources, R.color.colorPaint, null)
  1. At the class level, below, add a variable paint for a Paint object and initialize it as follows.
// Set up the paint with which to draw.
private val paint = Paint().apply {
   color = drawColor
   // Smooths out edges of what is drawn without affecting shape.
   isAntiAlias = true
   // Dithering affects how colors with higher-precision than the device are down-sampled.
   isDither = true
   style = Paint.Style.STROKE // default: FILL
   strokeJoin = Paint.Join.ROUND // default: MITER
   strokeCap = Paint.Cap.ROUND // default: BUTT
   strokeWidth = STROKE_WIDTH // default: Hairline-width (really thin)
}